package com.chexiehui.jc.ui.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.aries.ui.widget.alert.UIAlertDialog;
import com.blankj.utilcode.util.SizeUtils;
import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.FragmentViewPagerAdapter;
import com.chexiehui.jc.base.BaseActivity;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpActivity;
import com.chexiehui.jc.mvp.mine.BrowseHistoryCovenant;
import com.chexiehui.jc.mvp.mine.BrowseHistoryPresenter;
import com.chexiehui.jc.ui.fragment.BrowseHistoryForumFragment;
import com.chexiehui.jc.ui.fragment.BrowseHistoryPostsFragment;
import com.chexiehui.jc.ui.fragment.BrowseHistorySeriesFragment;
import com.chexiehui.jc.ui.fragment.IBrowseHistory;
import com.chexiehui.jc.ui.fragment.TempFragment;
import com.chexiehui.jc.utils.NoDoubleClickListener;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 浏览历史
 */
public class BrowseHistoryActivity extends BaseMvpActivity<BrowseHistoryPresenter>
        implements BrowseHistoryCovenant.View {

    @BindView(R.id.stl_action_table)
    SlidingTabLayout stlActionTable;
    @BindView(R.id.vp_action_pager)
    ViewPager vpActionPager;

    private String[] titles = new String[]{"车系", "论坛", "帖子"};
    private ArrayList<Fragment> actionFragmentList = new ArrayList<>();

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("浏览历史");
    }

    private void onDeleteTip() {
        int index = vpActionPager.getCurrentItem();
        String content = index == 0 ? "确定要清空车系浏览数据？" :
                index == 1 ? "确定要清空论坛浏览数据？" :
                        index == 2 ? "确定要清空论坛浏览数据？" : "确定要清空浏览数据";
                        new UIAlertDialog.DividerIOSBuilder(this)
                .setTitle("提示")
                .setTitleTextColorResource(R.color.colorAlertTitle)
                .setMessage(content)
                .setMessageTextColorResource(R.color.colorAlertMessage)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setNegativeButtonTextColorResource(R.color.colorAlertButton)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        int type = vpActionPager.getCurrentItem();
                        type = type == 0 ? 3 : type == 1 ? 2 : type == 2 ? 1 : 0;
                        mvpPresenter.onDoDelete(type);
                    }
                })
                .setMinHeight(SizeUtils.dp2px(120))
                .create()
                .setDimAmount(0.6f)
                .show();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_browse_history;
    }

    @Override
    protected void initView(Bundle var1) {
        vpActionPager.setOffscreenPageLimit(titles.length);
        actionFragmentList.add(BrowseHistorySeriesFragment.getInstance());
        actionFragmentList.add(BrowseHistoryForumFragment.getInstance());
        actionFragmentList.add(BrowseHistoryPostsFragment.getInstance());
        vpActionPager.setAdapter(new FragmentViewPagerAdapter(getSupportFragmentManager(), actionFragmentList, titles));
        stlActionTable.setViewPager(vpActionPager);

        titleBar.setRightText("清空")
                .setOnRightTextClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View view) {
                        int size = ((IBrowseHistory) actionFragmentList
                                .get(vpActionPager.getCurrentItem())).getDataSize();
                        if (size <= 0) {
                            showToast("暂无数据");
                        } else {
                            onDeleteTip();
                        }
                    }
                });
    }

    @Override
    protected BrowseHistoryPresenter createPresenter() {
        return new BrowseHistoryPresenter(this);
    }

    @Override
    public void onDoDeleteSuccess(BaseModel<Object> successResult) {
        showToast("已清空");
        ((IBrowseHistory) actionFragmentList
                .get(vpActionPager.getCurrentItem())).onClearRefresh();
    }

    @Override
    public void onDoDeleteFailure(BaseModel<Object> failureResult) {
        showToast(failureResult.getResultInfo());
    }
}
